PowerTCP Zip Compression for .NET
Data Compression



The QuickZip method is nice for simple zipping operations. For more sophisticated manipulation of an archive and its items, the Zip method can be used. Items can be added and removed using the Add and RemoveAt methods. Prior to zipping, individual items' properties, such as Password and Comment, can be set. The zipped archive destination can be a file or stream, according to the application needs.

Advanced zipping to a compressed file

  1. Add the Archive component to a new form.
  2. Add a button to the form.
  3. Add files or wildcard collections to the archive. The code below demonstrates three different methods for adding files. Begin by placing this code in the click event of the button added to the form in step 2
  4. One of the benefits of separating the Zip and Add operations is that the archive can be manipulated prior to the compression. For example, an item's comment can be set, or items can be removed from the archive using the RemoveAt method. To set the comment of the second item in the archive, and then remove the first item, place the following code directly after the code from step 3.
  5. Use the Zip method to create the zip file. Place the following code directly after the code from step 3.
  6. Compile and run the application. After pressing the button, a new archive "c:\test.zip" with the files added from "c:\Test" should be found on disk.
C#
Copy Code
//Add a file using the ArchiveItem constructor
ArchiveItem item = new ArchiveItem("c:\\Test\\file1.txt");
archive1.Add(item);
//Add a file using the filename. This could also be used for adding wildcard collections
archive1.Add("c:\\Test\\file2.log");
//Add a wildcard collection of files //In this case, all "txt" files in the directory "c:\Test" are added, except "file1.txt"
//Subfolders are not included, but the paths are preserved.
archive1.IncludeSubs = true;
archive1.ExcludePattern= "file1.txt";
archive1.PreservePath = true;
archive1.Add("c:\\Test\\*.txt");
if (archive1.Count > 1)
{
    //Set the comment for the second item (index 1)
    archive1[1].Comment = "This is the second item in the archive!";
    //Remove the first item (index 0)
    archive1.RemoveAt(0);
}
archive1.Zip("c:\\test.zip");
Visual Basic
Copy Code
'Add a file using the ArchiveItem constructor
Dim item As ArchiveItem = New ArchiveItem("c:\Test\file1.txt")
Archive1.Add(item)
'Add a file using the filename. This could also be used for adding wildcard collections
Archive1.Add("c:\Test\file2.log")
'Add a wildcard collection of files
'In this case, all "txt" files in the directory "c:\Test" are added, except "file1.txt"
'Subfolders are not included, but the paths are preserved.
Archive1.IncludeSubs = True
Archive1.ExcludePattern = "file1.txt"
Archive1.PreservePath = True
Archive1.Add("c:\Test\*.txt")
If Archive1.Count > 1 Then
    'Set the comment for the second item (index 1)
    Archive1(1).Comment = "This is the second item in the archive, soon to be first!"
    'Remove the first item (index 0)
    Archive1.RemoveAt(0)
End If
Archive1.Zip("c:\test.zip")

Advanced zipping to a compressed stream

  1. Follow steps 1, 2 , 3 and 4 above, for zipping to a compressed file.
  2. Create a stream to hold the compressed archive.
  3. Use the Zip method to save the compressed archive to memory.
  4. Show the length of the new compressed archive.
  5. Compile and run the application. After pressing the button, a MessageBox with the size of the stream should be visible.
C#
Copy Code
System.IO.MemoryStream stream = new System.IO.MemoryStream();
archive1.Zip(stream);
MessageBox.Show("The size of the compressed stream is " + stream.Length.ToString() + " bytes.");
Visual Basic
Copy Code
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream()
Archive1.Zip(stream)
MessageBox.Show("The size of the compressed stream is " + stream.Length.ToString() + "bytes.")

Advanced asynchronous zipping

  1. Follow steps 1, 2, 3, and 4 above, for zipping to a compressed file.
  2. Use the asynchronous BeginZip method to create the zip file.
  3. Add code for the EndZip Event. When using the BeginXXX methods, you must have corresponding EndXXX events (see Asynchronous Operation for more details).
  4. Compile and run the application. After pressing the button, a new archive "c:\test.zip" with the files from "c:\Test" should be found on disk.
C#
Copy Code
archive1.BeginZip("c:\\test.zip", null);
...
private void archive1_EndZip(object sender, Dart.PowerTCP.Zip.EndEventArgs e)
{
    try
    {
        if (e.Exception == null) MessageBox.Show("Zip Complete!");
        else MessageBox.Show(e.Exception.Message);
    }
    catch {}
}
Visual Basic
Copy Code
Archive1.BeginZip("c:\test.zip", Nothing)
...
Private Sub Archive1_EndZip(ByVal sender As Object, ByVal e As Dart.PowerTCP.Zip.EndEventArgs) Handles Archive1.EndZip
    Try
        If (e.Exception Is Nothing) Then MessageBox.Show("Zip Complete!")
        Else MessageBox.Show(e.Exception.Message)
        End If
    Catch
    End Try
End Sub

PowerTCP Zip for .NET Documentation Version 2.1.1
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic